package cn.kk.Blog.controller.system;
/*
 *@author KK on 2022.12.01 21:08
 */

import cn.kk.Blog.core.servlet.BaseServlet;
import cn.kk.Blog.entity.TSystemUser;
import cn.kk.Blog.service.ISystemUserService;
import cn.kk.Blog.service.impl.SystemUserServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/SystemUserServlet/*")
public class SystemUserServlet extends BaseServlet {
    private ISystemUserService systemUserService = new SystemUserServiceImpl();

    public void list() throws Exception {
        // 1.查询数据
        List<TSystemUser> list = systemUserService.list();
        // 2.把数据放到Map中
        Map<String, Object> map = new HashMap<>();
        map.put("sysUserList", list);
        // 3.跳转到视图
        outView("system/sysUserList", map);
    }

    public void getUserByName() throws SQLException, IOException {
        String username = super.req.getReader().readLine();
        TSystemUser user = systemUserService.selectByName(username);
        String jsonString = JSON.toJSONString(user);
        super.resp.getWriter().write(jsonString);
    }

    public String add() throws Exception {
        String json = super.req.getReader().readLine();
        TSystemUser systemUser = JSON.parseObject(json, TSystemUser.class);
        boolean save = systemUserService.save(systemUser);
        return outJson(save);
    }
}
